package com.cbg.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cbg.base.constant.Constant;
import com.cbg.sys.entity.Car;
import com.cbg.sys.entity.CarType;
import com.cbg.sys.entity.Line;
import com.cbg.sys.entity.LineCar;
import com.cbg.sys.mapper.LineCarMapper;
import com.cbg.sys.service.ICarService;
import com.cbg.sys.service.ICarTypeService;
import com.cbg.sys.service.ILineCarService;
import com.cbg.base.core.SuperServiceImpl;
import com.cbg.sys.service.ILineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 路线车辆表 服务实现类
 * </p>
 *
 * @author chenbing
 * @since 2018-12-17
 */
@Service
public class LineCarServiceImpl extends SuperServiceImpl<LineCarMapper, LineCar> implements ILineCarService {
    @Autowired
    ICarTypeService typeService;
    @Autowired
    StringRedisTemplate counter;
    @Autowired
    ICarService carService;
    @Autowired
    ILineService lineService;
    /**
     * 排车型、车辆
     */
    @Override
    public void planTypeAndCar(List<Line> lines){
        List<CarType> types = typeService.list();
        lines.forEach(line->types.forEach(type->{
            String typeId = type.getId();
            Long count=counter.opsForValue().increment(Constant.TYPE_SUFFIX + typeId, 1);
            List<Car> cars = carService.list(new LambdaQueryWrapper<Car>().eq(Car::getTypeId, typeId));
            int carIndex =(int)((count-1) % cars.size());
            new LineCar(line.getId(),typeId,cars.get(carIndex).getId(),0,0).insert();
        }));
    }
}
